package com.rhzy.wnhc.controller;


import com.baomidou.mybatisplus.core.conditions.query.LambdaQueryWrapper;
import com.rhzy.wnhc.entity.LoginVo;
import com.rhzy.wnhc.entity.TAdmin;
import com.rhzy.wnhc.entity.TAreacode;
import com.rhzy.wnhc.entity.TAuthCode;
import com.rhzy.wnhc.service.TAreacodeService;
import com.rhzy.wnhc.service.TAuthCodeService;
import com.rhzy.wnhc.util.AjaxResult;
import com.rhzy.wnhc.util.RudmUtils;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiOperation;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.transaction.annotation.Transactional;
import org.springframework.util.StringUtils;
import org.springframework.validation.annotation.Validated;
import org.springframework.web.bind.annotation.*;

import org.springframework.stereotype.Controller;

import java.util.ArrayList;
import java.util.List;

/**
 * <p>
 *  授权码前端控制器
 * </p>
 *
 * @author wen
 * @since 2020-02-22
 */
@Api(tags = "授权码管理")
@RestController
@RequestMapping("/authCode")
public class TAuthCodeController {


    @Autowired
    TAuthCodeService codeService;
    @Autowired
    TAreacodeService areacodeService;

    @ApiOperation(value = "生成授权码", tags = {"认证管理",})
    @RequestMapping(value = "/createCode/{areacode}", method = RequestMethod.GET)
    @Transactional
    public AjaxResult addWishJob(@PathVariable String areacode){
        try{
            areacode=areacode.substring(0,6);
            TAuthCode authCode=codeService.getOne(new LambdaQueryWrapper<TAuthCode>().likeRight(TAuthCode::getAreacode,areacode).last(" limit 1 "));

            if(authCode!=null){
                return AjaxResult.addError("当前地区已配置授权码");
            }
            List<TAreacode> areacodeList=areacodeService.list(new LambdaQueryWrapper<TAreacode>().eq(TAreacode::getAreaTag,5).likeRight(TAreacode::getAreaCode,areacode));
            if(areacodeList!=null&&areacodeList.size()>0){
                List<TAuthCode> tAuthCodes=new ArrayList<>();
                authCode=codeService.getOne(new LambdaQueryWrapper<TAuthCode>().orderByDesc(TAuthCode::getAreacode).last(" limit 1 "));
                if(authCode==null){
                    authCode=new TAuthCode();
                    authCode.setAreacode("100000");
                }
                int start=Integer.parseInt(authCode.getAreacode());
                int[] arr= RudmUtils.randomSet(start,start+areacodeList.size()+1000,areacodeList.size());
                for (int i=0;i<arr.length;i++){
                    TAuthCode tAuthCode=new TAuthCode();
                    tAuthCode.setAreacode(areacodeList.get(i).getAreaCode());
                    tAuthCode.setAreaInfo(areacodeList.get(i).getAreaInfo());
                    tAuthCode.setAuthCode(arr[i]+"");
                    tAuthCodes.add(tAuthCode);
                }
                codeService.saveBatch(tAuthCodes);
                return AjaxResult.addSuccess();
            }
            return AjaxResult.addError("该地址下没有村信息");

        }catch (Exception e){
            e.printStackTrace();
            return AjaxResult.addError("异常："+e.getMessage());
        }
    }
}
